var resource = require('../model/resource');

exports.selectAll = function(req, res, connection) {
	var cpage = req.query.index;
	var pageSize = req.query.item;
	var begin = pageSize * (cpage - 1);
	var end = pageSize * cpage - 1;
	resource.selectResource(connection, function(rows) {
		if(cpage == '' || cpage == null || cpage == undefined){
			res.send(rows);
			return;
		}
		var data = [];
		var k = 0;
		for (var i = begin; i < end + 1; i++) {
			if (i < rows.length) {
				data[k] = rows[i];
				k++;
			}
		}
		var sendData = {
			data: data,
			count: rows.length
		}
		res.send(sendData);
	});
};

exports.addResource = function(req, res, connection) {
  var data = req.body;
  resource.addResource(connection, data, function(rows){
  	res.send(rows);
  });
};

exports.updateResource = function(req, res, connection){
	var data = req.body;
	resource.updateResource(connection, data, function(rows){
		res.send(rows);
	});
};

exports.deleteResource = function(req, res, connection){
	var data = req.body;
	resource.deleteResource(connection, data.id, function(rows){
		res.send(rows);
	});
}